#include "Common.hpp"

int main(int argc,char* argv[]){
    if (argc != 3){
        Useage("Please use: ./clietn + server_ip + server_port");
        exit(1);
    }
    int fd = ::socket(AF_INET,SOCK_DGRAM,0);
    if (fd < 0){
        LOG(ERROR) << "socket create error";
        exit(fd);
    }
    //初始化服务端信息, 注意这里不是用来bind的, 而是直接用来发送的
    sockaddr_in server;
    string ip = argv[1];
    uint16_t port = stoi(argv[2]);  
    server.sin_family = AF_INET;
    server.sin_port = ::htons(port);
    server.sin_addr.s_addr = ::inet_addr(ip.c_str());
    socklen_t len = sizeof(server);

    string messgae;
    while(1){
        LOG(INFO) << "Please enter";
        getline(cin,messgae);
        if (messgae.size() == 0) messgae += '\n';
        sendto(fd,messgae.c_str(),messgae.size(),0,CONV(&server),len);
        sockaddr_in peer;
        socklen_t Len = sizeof(peer);
        char inbuffer[1024];
        ssize_t n  =  recvfrom(fd,inbuffer,1024,0,CONV(&peer),&Len);
        if (n > 0){
            inbuffer[n] = 0;
            LOG(INFO)  << inbuffer;
        }
    }
    return 0;
}